package com.xlhj.jpa.controller;

import com.xlhj.jpa.entity.SysUser;
import com.xlhj.jpa.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserController
 * @Description 用户表控制器
 * @Author liucaijing
 * @Date 2021/2/24 16:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService userService;

    @PostMapping("/insertUser")
    public SysUser insertUser(@RequestBody SysUser user) {
        return userService.insertUser(user);
    }

    @GetMapping("/selectAllUser")
    public List<SysUser> selectAllUser() {
        return userService.selectAllUser();
    }

    @GetMapping("/selectUserById")
    public SysUser selectUserById(Long id) {
        return userService.selectUserById(id);
    }

    @GetMapping("/selectUserByPage")
    public Page<SysUser> selectUserByPage(int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<SysUser> userPage = userService.selectUserByPage(pageable);
        return userPage;
    }

    @GetMapping("/findByUserNameAndPage")
    public Page<SysUser> findByUserNameAndPage(String userName, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<SysUser> userPage = userService.findByUserNameAndPage(userName, pageable);
        return userPage;
    }

    @GetMapping("/findByUserCode")
    public SysUser findByUserCode(String userCode) {
        return userService.findByUserCode(userCode);
    }

    @GetMapping("/findByUserName")
    public List<SysUser> findByUserName(String userName) {
        return userService.findByUserName(userName);
    }

    @PutMapping("/updateUser")
    public SysUser updateUser(@RequestBody SysUser user) {
        return userService.updateUser(user);
    }

    @GetMapping("/deleteUserById")
    public void deleteUserById(Long id) {
        userService.deleteUserById(id);
    }
}
